package svc

import (
	"go.uber.org/zap"
	"gorm.io/gorm"
	"img_analyse/analyse/api/internal/config"
	"img_analyse/analyse/model"

	"img_analyse/commn/init_gorm"
	"img_analyse/middleware/Logger"
	"net/http"
)

type ServiceContext struct {
	Request *http.Request
	Config  config.Config
	DB      *gorm.DB
}

func NewServiceContext(c config.Config) *ServiceContext {
	// 初始化GORM
	db := init_gorm.InitGorm(c.DB.Dsn)

	// 自动迁移表结构
	err := db.AutoMigrate(&model.Image{})
	if err != nil {
		logger.Logger.Error("AutoMigrate error", zap.Error(err))
		panic("AutoMigrate error")
	}
	return &ServiceContext{
		Config: c,
		DB:     db,
	}
}
